package com.hu.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.common.pojo.Role;
import com.hu.common.pojo.RoleMenu;
import com.hu.user.mapper.RoleMapper;
import com.hu.user.mapper.RoleMenuMapper;
import com.hu.user.service.IRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hu
 * @since 2023-08-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private RoleMapper roleMapper;

    // 实现思路： 先删后增
    @Transactional   // 事务，保证删除和重新赋值同成功或失败
    @Override
    public void setRoleMenu(Integer roleId, String menuIds) {
        // 先删除当前id所有的绑定关系
        roleMenuMapper.deleteByRoleId(roleId);

        // 再把前端传来的菜单ids绑定给当前的角色id
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenu.setMenuIds(menuIds);
        roleMenuMapper.insert(roleMenu);
    }
    @Override
    public String getMenuById(Integer roleId) {
        return roleMenuMapper.selectMenuByRoleId(roleId);
    }

    @Override
    public List<Role> queryRole(Role role, Integer orderStatus, String columnName) {
        if (StringUtils.isEmpty(columnName)) {
            columnName = "id";
        }
        return roleMapper.queryRole(role, orderStatus, columnName);
    }
}
